package org.mclll.xwangplugin.cartography.recipes;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

/**
 * @author llB
 * @date 2024-06-15 16:33
 * @description todo
 */
public abstract class CartographyCustomRecipes {

    protected final int MAIN_SORT_INDEX = 0;
    protected final int MATERIALS_SORT_INDEX = 1;
    protected final int RESULT_SORT_INDEX = 2;

    public void toCreate(InventoryClickEvent event, Inventory cartographyInventory) {
        Player player = ((Player) event.getWhoClicked()).getPlayer();
        // 点击的物品，可能为null，null为无效点击
        ItemStack clickedItem = event.getCurrentItem();
        // 指针上的物品，不会为null，没有东西就是AIR
        ItemStack cursor = event.getCursor();
        // 点击类型
        ClickType click = event.getClick();
        // 点击的容器格类型
        InventoryType.SlotType slotType = event.getSlotType();
        // 点击的格子号
        int clickSlot = event.getSlot();

        boolean updateMainTransferReelClick = slotType == InventoryType.SlotType.CRAFTING && clickSlot == MAIN_SORT_INDEX && isMainMaterial(cursor) && (click == ClickType.LEFT || click == ClickType.RIGHT);
        boolean updateMainTransferReelShiftClick = isMainMaterial(clickedItem) && click.isShiftClick() && slotType != InventoryType.SlotType.CRAFTING;

        boolean updateEmptyReelClick = slotType == InventoryType.SlotType.CRAFTING && clickSlot == MATERIALS_SORT_INDEX && isViceMaterial(cursor) && (click == ClickType.LEFT || click == ClickType.RIGHT);
        boolean updateEmptyReelShiftClick = isViceMaterial(clickedItem) && click.isShiftClick() && slotType != InventoryType.SlotType.CRAFTING;

        // 单机物品到格子
        if (updateMainTransferReelClick || updateEmptyReelClick) {
            cartographyInventory.setItem(clickSlot, cursor.clone());
            // 格子里有东西就交换，没有就清空
            if (clickedItem != null && clickedItem.getType() != Material.AIR) {
                player.setItemOnCursor(clickedItem);
            } else {
                player.setItemOnCursor(new ItemStack(Material.AIR));
            }
            player.updateInventory();
            event.setCancelled(true);
        }
        // Shift单机物品到格子
        else if (updateMainTransferReelShiftClick || updateEmptyReelShiftClick) {
            int sort = updateMainTransferReelShiftClick ? MAIN_SORT_INDEX : MATERIALS_SORT_INDEX;
            ItemStack itemInSort = cartographyInventory.getItem(sort);
            if (itemInSort != null && itemInSort.getType() != Material.AIR) {
                return;
            }
            cartographyInventory.setItem(sort, clickedItem.clone());
            player.getInventory().setItem(clickSlot, new ItemStack(Material.AIR));
            player.updateInventory();
            event.setCancelled(true);
        }
        // 添加合成物品到Result格
        if (event.isCancelled() && isMainMaterial(cartographyInventory.getItem(MAIN_SORT_INDEX)) && isViceMaterial(cartographyInventory.getItem(MATERIALS_SORT_INDEX))) {
            ItemStack result = generalResultItem(cartographyInventory);
            cartographyInventory.setItem(RESULT_SORT_INDEX, result);
            player.updateInventory();
        }

        if (slotType == InventoryType.SlotType.RESULT && isResultItem(clickedItem)) {
            // 合成后清除物品
            ItemStack mainItem = cartographyInventory.getItem(MAIN_SORT_INDEX);
            ItemStack viceItemItem = cartographyInventory.getItem(MATERIALS_SORT_INDEX);

            boolean isSuccessCreate = true;
            if (click.isShiftClick()) {
                player.getInventory().addItem(clickedItem);
            } else if (cursor.getType() == Material.AIR || cursor.getAmount() < cursor.getMaxStackSize()) {
                if (isResultItem(cursor)) {
                    cursor.setAmount(cursor.getAmount() + 1);
                } else {
                    player.setItemOnCursor(clickedItem);
                }
            } else {
                isSuccessCreate = false;
            }
            if (isSuccessCreate && clearMain()) {
                if (mainItem.getAmount() > 1) {
                    mainItem.setAmount(mainItem.getAmount() - 1);
                } else {
                    mainItem.setType(Material.AIR);
                }
                cartographyInventory.setItem(MAIN_SORT_INDEX, mainItem);
            }
            if (isSuccessCreate && clearVice()) {
                if (viceItemItem.getAmount() > 1) {
                    viceItemItem.setAmount(viceItemItem.getAmount() - 1);
                } else {
                    viceItemItem.setType(Material.AIR);
                }
                cartographyInventory.setItem(MATERIALS_SORT_INDEX, viceItemItem);
            }
            event.setCancelled(true);
        }
    }


    abstract protected boolean isMainMaterial(ItemStack item);

    abstract protected boolean isResultItem(ItemStack item);

    abstract protected boolean isViceMaterial(ItemStack item);

    abstract protected ItemStack generalResultItem(Inventory cartographyInventory);

    abstract protected boolean clearMain();

    abstract protected boolean clearVice();
}
